package com.eascs.datalist.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.admin.api.form.O2oAfterLoanForm;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.o2ozx.dao.O2oZxDao;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.datalist.dao.O2oDataListDao;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.file.FileTypeConstants;

@Service
public class O2oDataListServiceImpl implements O2oDataListService {
	private static Logger logger = LoggerFactory.getLogger(O2oDataListServiceImpl.class);
	@Autowired
	O2oDataListDao o2oDataListDao;
	@Autowired
	O2oZxDao o2oZxDao;
	@Override
	public PageResult<Object[]> findByPage(SqlPageQuery<Object[]> pageQuery) {

		return o2oDataListDao.findDataWithPage(pageQuery);
	}

	@Override
	public List<O2oDataListEntity> findByIdList(String id) {
		String sql = "select * from o2o_datalist od where od.is_delete=? and  od.loanapply_no=?";
		return o2oDataListDao.findEntitys(sql, O2oDataListEntity.class, StatusContexts.O2O_NO, id);
	}

	@Override
	@Transactional(transactionManager = "o2oTransactionManager")
	public int updateEntityById(O2oDataListEntity loanEntity) {
		return o2oDataListDao.updateById(loanEntity);

	}

	@Transactional(transactionManager = "o2oTransactionManager")
	@Override
	public int deleteEntityById(O2oDataListEntity loanEntity) {
		// loanEntity = findById(loanEntity.getDatalistNo());
		return o2oDataListDao.updateById(loanEntity);
	}

	@Override
	@Transactional(transactionManager = "o2oTransactionManager")
	public int saveEntity(O2oDataListEntity loanEntity) {
		// loanEntity.setDatalistNo(UuidUtil.generateShortUuid(4));
		return o2oDataListDao.insert(loanEntity);
	}

	@Override
	public O2oDataListEntity findO2oDataListEntity(String datalistno) {
		String sql = "select * from o2o_datalist od where od.is_delete=? and od.datalist_no=?";
		return o2oDataListDao.findEntity(sql, O2oDataListEntity.class, StatusContexts.O2O_NO, datalistno);
	}

	@Override
	public List<O2oDataListEntity> findByEntityList(String loanapplyNo) {
		String sql = "select * from o2o_datalist od where od.is_delete=? and od.loanapply_no=?";
		return o2oDataListDao.findEntitys(sql, O2oDataListEntity.class, StatusContexts.O2O_NO, loanapplyNo);
	}
	
	@Override
	public List<O2oDataListEntity> findByEntityListNZX(String loanapplyNo,String zxno) {
		String sql = "select * from o2o_datalist od where od.is_delete=? and od.loanapply_no in (?,?) ";
		return o2oDataListDao.findEntitys(sql, O2oDataListEntity.class, StatusContexts.O2O_NO, loanapplyNo,zxno);
	}

	@Override
	public List<O2oDataListEntity> findByEntityList(String loanapplyNo,String sponsorId) {
		String sql = "select * from o2o_datalist od where od.is_delete=? and od.loanapply_no=? and od.sponsor_id = ? ";
		return o2oDataListDao.findEntitys(sql, O2oDataListEntity.class, StatusContexts.O2O_NO, loanapplyNo,sponsorId);
	}
	
	/**
	 * 根据贷款单号和附件类型查询附件信息
	 * 
	 * @param id
	 * @return
	 */
	public List<O2oDataListEntity> getByBillCodeDataList(String loanapplyNo, String paymentsId) {
		String sql = "select * from o2o_datalist od where od.is_delete=? and od.loanapply_no=? and od.datalist_code=? and od.payments_id=?";
		return o2oDataListDao.findEntitys(sql, O2oDataListEntity.class, StatusContexts.O2O_NO, loanapplyNo, FileTypeConstants.DALIST_TYPE_RPMENT, paymentsId);
	}

	/**
	 * 查询Code 判断是否附件上传完整
	 */
	@Override
	public boolean checkDataCode(String loanapplyNo, String mater) {
		boolean flag = true;
		String sql = "select d.datalist_code from o2o_datalist d where d.is_delete=? and d.loanapply_no=?";
		List<String> code = o2oDataListDao.findSimpleObjectList(sql, String.class, StatusContexts.O2O_NO, loanapplyNo);
		if (code != null && code.size() > 0) {
			if (!code.contains(FileTypeConstants.DALIST_TYPE_IDC) || !code.contains(FileTypeConstants.DALIST_TYPE_IDRC) || !code.contains(FileTypeConstants.DALIST_TYPE_CRD)|| !code.contains(FileTypeConstants.DALIST_TYPE_Loaner_Warrant)|| !code.contains(FileTypeConstants.DALIST_TYPE_Loaner_Warrant_IDCARD)) {
				flag = false;
			}
			O2oZxEntity o2ozx=o2oZxDao.getById(O2oZxEntity.class, loanapplyNo);
			if("ZX_JKR".equals(o2ozx.getZxType()) && "SPDBGZ".equals(o2ozx.getZxBank())&&!code.contains(FileTypeConstants.DALIST_TYPE_ApplyCredit_Intent)  ){
				flag = false;
			}
		
			if (mater != null) {
				if (!code.contains(FileTypeConstants.DALIST_TYPE_MATE_IDC) || !code.contains(FileTypeConstants.DALIST_TYPE_MATE_IDRC) || !code.contains(FileTypeConstants.DALIST_TYPE_MATE_CRD)) {
					flag = false;
				}
			}
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkZxAttachment(String loanapplyNo, String mater) {
		boolean flag = true;
		String sql = "select d.datalist_code from o2o_datalist d where d.is_delete=? and d.loanapply_no=?";
		List<String> code = o2oDataListDao.findSimpleObjectList(sql, String.class, StatusContexts.O2O_NO, loanapplyNo);
		if (code != null && code.size() > 0) {
			if (!code.contains(FileTypeConstants.DALIST_TYPE_ZXBGDATA)) {
				flag = false;
			}
			if (mater != null) {
				if (!code.contains(FileTypeConstants.DALIST_TYPE_ZXMATE)) {
					flag = false;
				}
			}
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkLoanApply(String loanApplyNo) {
		boolean flag = true;
		String sql = "SELECT ol.loana_mount,ol.loan_term,ol.repay_mentway,ol.guar_antee_way,ol.ltd,ol.operator_no from o2o_loanapply ol  where ol.loanapply_no = ?";
		List<Object[]> code = o2oDataListDao.findObjectsList(sql, loanApplyNo);
		if (code != null && code.size() > 0) {
			for (int i = 0; i < code.size(); i++) {
				for (int j = 0; j < code.get(i).length; j++) {
					if (StringUtil.isEmpty(code.get(i)[j])) {
						flag = false;
					}
				}
			}
		}
		return flag;
	}

	@Override
	public boolean checkUserInfo(String loanApplyNo) {
		boolean flag = true;
		String sql = "SELECT OLU.LOAN_ID, OLU.USER_SURNAME, OLU.USER_NAME, OLU.USER_FULL_NAME, OLU.USER_NAME_PINYIN, OLU.SEX, OLU.DOCUMENT_TYPE, OLU.DOCUMENT_NO, OLU.USER_PHONE, OLU.LOANAPPLY_NO FROM O2O_LOAN_USER OLU JOIN O2O_LOANAPPLY OL ON OL.LOAN_ID = OLU.LOAN_ID WHERE OL.LOANAPPLY_NO = ? ";
		List<Object[]> code = o2oDataListDao.findObjectsList(sql, loanApplyNo);
		if (code != null && code.size() > 0) {
			for (int i = 0; i < code.size(); i++) {
				for (int j = 0; j < code.get(i).length; j++) {
					if (StringUtil.isEmpty(code.get(i)[j])) {
						flag = false;
					}
				}
			}
		}
		return flag;
	}

	/**
	 * 校验是否上传附件
	 */
	@Override
	public boolean checkCodeAttachment(String loanapplyNo, String code) {
		boolean flag = true;
		String sql = "select d.datalist_code from o2o_datalist d where d.is_delete=? and d.loanapply_no=? and d.datalist_code=?";
		List<String> codelist = o2oDataListDao.findSimpleObjectList(sql, String.class, StatusContexts.O2O_NO, loanapplyNo, code);
		logger.info("查询附件次数：" + codelist.size());
		if (codelist != null && codelist.size() > 0) {
			if (!codelist.contains(code)) {
				flag = false;
			}
		} else {
			flag = false;
		}
		logger.info("查询flag：" + flag);
		return flag;
	}
	
	
	
	/**
	 * 根据loanapplyNo和Code查询实体
	 */
	@Override
	public O2oDataListEntity findCode(String loanapplyNo, String code) {
		return o2oDataListDao.findCode(loanapplyNo, code);
	}
	
	
	/**
	 * @Title: isDatalistExsit
	 * @Description: 检查附件是否已经上传
	 * @author 张庆奇
	 * @param loanapplyNo
	 * @param code
	 * @return
	 * @throws
	 * @date 2017年10月26日 下午3:08:02
	 */
	@Override
	public boolean isDatalistExsit(String loanapplyNo, String code) {
		boolean flag=true;
		String sql = "select count(1) from o2o_datalist d where d.is_delete=? and d.loanapply_no=? and d.datalist_code=?";
		int num = o2oDataListDao.findInteger(sql, StatusContexts.O2O_NO, loanapplyNo, code);
		if (num > 0) {
				flag = false;
		}
		return flag;
	}
	
	/**
	 * 根据loanapplyNo,Code,sponsorId(担保人id)和查询实体
	 */
	@Override
	public O2oDataListEntity findCode(String loanapplyNo, String code,String sponsorId) {
		return o2oDataListDao.findSponsorCode(loanapplyNo, code,sponsorId);
	}

	/**
	 * @Title: getCreditByPage
	 * @Description: 征信报告查询 
	 * @author 张庆奇
	 * @param entity
	 * @return
	 * @throws
	 * @date 2017年2月8日 下午3:25:35
	 */
	@Override
	public PageResult<Object[]> getCreditByPage(O2oAfterLoanForm entity) {
		
		return o2oDataListDao.getCreditByPage(entity);
	}
	
	/**
	 * @Title: getCreditByPage
	 * @Description: 企业征信报告附件是否上传齐全 
	 * @author yy
	 * @param 
	 * @return
	 * @throws
	 */
	@Override
	public boolean checkQYZXApply(String id) {
		boolean flag = true;
		String sql = "select d.datalist_code from o2o_datalist d where d.is_delete=? and d.loanapply_no=?";
		List<String> code = o2oDataListDao.findSimpleObjectList(sql, String.class, StatusContexts.O2O_NO, id);
		if (code != null && code.size() > 0) {
			if (!code.contains(FileTypeConstants.ENTERPRISE_ZX_IDC) 
					|| !code.contains(FileTypeConstants.ENTERPRISE_ZX_IDRC) 
					|| !code.contains(FileTypeConstants.ENTERPRISE_ZX_CRD)
					|| !code.contains(FileTypeConstants.ENTERPRISE_ZX_ICRD)) {
				flag = false;
			}
		} else {
			flag = false;
		}
		return flag;
	}

	@Override
	public boolean checkPFDataCode(String loannapplyNo) {
		boolean flag = true;
		String sql = " select d.datalist_code from o2o_datalist d where d.is_delete = 'N' and d.loanapply_no = ? ";
		List<String> code = o2oDataListDao.findSimpleObjectList(sql, String.class, loannapplyNo);
		if (code != null && code.size() > 0) {
			if (!code.contains(FileTypeConstants.DALIST_TYPE_ApplyCredit_Intent)) {
				flag = false;
			}
		} else {
			flag = false;
		}
		return flag;
	}
}
